<template>
  <div class="app-container customer-div">
    <div style="margin-top: 40px">
      <!--搜索-->
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">

        <el-form-item label="供应商：">
          <el-select
                  clearable
                  v-model="listQuery.supplierId"
                  filterable
                  reserve-keyword
                  placeholder="请输入关键词"
                  style="width: 220px;">
            <el-option
                    v-for="item in options2"
                    :key="item.supplierId"
                    :label="item.supplierName"
                    :value="item.supplierId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="采购订单号：">
          <el-input v-model="listQuery.orderSn" clearable></el-input>
        </el-form-item>

        <el-form-item label="采购员：">
          <el-select v-model="listQuery.userId" filterable placeholder="请选择" clearable>
            <el-option
                    v-for="item in userOptionList"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="开票状态：" >
          <el-select v-model="listQuery.invoiceState" placeholder="请选择" clearable >
            <el-option
                    :key="0"
                    label="未开票"
                    :value="0">
            </el-option>
            <el-option
                    :key="1"
                    label="部分开票"
                    :value="1">
            </el-option>
            <el-option
                    :key="2"
                    label="已开票"
                    :value="2">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="采购日期：">
          <el-date-picker
                  v-model="listQuery.orderDate"
                  type="daterange"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="发票号：">
          <el-input v-model="listQuery.invoiceNo" clearable></el-input>
        </el-form-item>

        <el-form-item label="品名：">
          <el-input v-model="listQuery.goodsName" clearable></el-input>
        </el-form-item>

        <el-form-item label="牌号：" >
          <el-select v-model="listQuery.sapGoodsNo" filterable placeholder="请选择" clearable>
            <el-option
                    v-for="item in goodsList"
                    :key="item.kingNumber"
                    :label="item.goodsSn"
                    :value="item.kingNumber">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="入库单号：" >-->
<!--          <el-input v-model="listQuery.entryOrderSn"></el-input>-->
<!--        </el-form-item>-->

        <el-form-item label="合同公司：" >
          <el-input v-model="listQuery.fromCompany" clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-button
                  @click="handleSearchList()"
                  type="primary">
            查询
          </el-button>
          <el-button
                  v-if="exportPurchaseReceipt"
                  :loading="btnLoading2"
                  @click="downloadExcel()">
            导出
          </el-button>
        </el-form-item>

      </el-form>
    </div>

    <!--数据列表-->
    <div class="table-container">
      <div style="font-size:14px;line-height: 30px;padding:10px 0 ">
        入库总额：<span style="font-weight: bold">{{orderAmount}}</span>
        <span style="padding-left:20px;">已开票：</span><span style="font-weight: bold">{{hasInvoiceAmount}}</span>
        <span style="padding-left:20px;">未开票：</span><span style="font-weight: bold">{{unInvoiceAmount}}</span>
        <span style="padding-left:20px;">入库总量：</span><span style="font-weight: bold">{{goodsNum}}</span>
      </div>
      <el-table
              v-loading="listLoading"
              :data="tableData"
              border
              style="width: 100%; margin-top: 20px">
        <el-table-column  label="采购单号" min-width="180" align="center" prop="orderSn">
        </el-table-column>
        <el-table-column  label="采购日期" min-width="100" align="center" prop="orderDate">
        </el-table-column>
        <el-table-column  label="供应商" min-width="300" align="center" prop="supplierName">
        </el-table-column>
        <el-table-column  label="采购员" min-width="100" align="center" prop="userName">
        </el-table-column>
        <el-table-column  label="品名" min-width="120" align="center">
          <template slot-scope="scope">
            <span style="font-size:12px;vertical-align: middle">{{scope.row.goodsName}}</span>
          </template>
        </el-table-column>
        <el-table-column
                label="牌号"
                align="center"
                min-width="150">
          <template slot-scope="scope">
            {{scope.row.goodsSn}}
          </template>
        </el-table-column>
        <el-table-column  label="合同数量" min-width="100" align="center">
          <template slot-scope="scope">
            {{scope.row.goodsNum}}
          </template>
        </el-table-column>
        <el-table-column
                align="center"
                label="合同单价"
                min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsPrice}}
          </template>
        </el-table-column>
        <el-table-column
                align="center"
                label="合同金额"
                min-width="100">
          <template slot-scope="scope">
            {{scope.row.orderAmount}}
          </template>
        </el-table-column>
        <el-table-column
                align="center"
                label="合同公司"
                min-width="150">
          <template slot-scope="scope">
            {{scope.row.fromCompany}}
          </template>
        </el-table-column>
<!--        <el-table-column  label="入库单号" min-width="150" align="center" prop="entryOrderNo">-->
<!--           <template slot-scope="scope">-->
<!--               <div @click="toDetail(scope.row)" style="color:#409EFF;cursor: pointer;vertical-align: middle">-->
<!--                  {{scope.row.entryOrderNo}}-->
<!--               </div>-->
<!--           </template>-->
<!--        </el-table-column>-->
        <el-table-column
                label="入库数量"
                align="center"
                min-width="100">
          <template slot-scope="scope">
            {{scope.row.goodsInNum}}
          </template>
        </el-table-column>
        <el-table-column
                label="入库金额"
                align="center"
                min-width="100">
          <template slot-scope="scope">
            <span> {{scope.row.inOrderAmount}}</span>

          </template>
        </el-table-column>
        <el-table-column
                label="已开票金额"
                min-width="100"
                align="center">
          <template slot-scope="scope">
            <span> {{scope.row.invouceAmount}}</span>
          </template>
        </el-table-column>
        <el-table-column
                label="未开票金额"
                min-width="100"
                align="center">
          <template slot-scope="scope">
            {{scope.row.unInvouceAmount}}
          </template>
        </el-table-column>

        <el-table-column
                label="开票状态"
                min-width="100"
                align="center">
          <template slot-scope="scope">
            {{scope.row.invoiceState }}
          </template>
        </el-table-column>
        <el-table-column
                label="发票号"
                min-width="120"
                align="center">
          <template slot-scope="scope">
            {{scope.row.invoiceSn}}
          </template>
        </el-table-column>

      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              layout="total, sizes,prev, pager, next,jumper"
              :current-page.sync="listQuery.pageNum"
              :page-size="listQuery.pageSize"
              :page-sizes="[10,15,20]"
              :total="count">
      </el-pagination>
    </div>

  </div>
</template>
<script>
  import {getCustomerOption,supplierList,getOrderUsersList,getDeliverDetailList,exportDeliverDetailList,orderGoodsList,invoiceListPurchase,} from '@/api/partner'
  import {formatDate} from '@/utils/date';
  import store from '@/store'
  import { mapGetters } from 'vuex'
  const defaultListQuery={
    "supplierId":'',
    "orderSn" : "",
    "userId" : '', //采购员
    "invoiceState" : '', //开票状态(0-未开票，1-部分开票，2-已开票)
    "invoiceNo" : "",
    "goodsName" : "",
    "sapGoodsNo" : "",
    orderDate:[],
    "pageNum" : 1,
    "pageSize" : 10,
    "isExport":0, //是否导出(1是，0否)
    fromCompany:''
  };
  export default {
    name: "index",
    data(){
      return {
        listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
        tableData: [],
        count: 0,
        listLoading: false,
        roles:store.getters.roles ? store.getters.roles : [],
        userOptionList:[],
        orderAmount:'',
        hasInvoiceAmount:'',
        unInvoiceAmount:'',
        btnLoading2:false,
        options2:[],
        goodsList:[],
        goodsNum:''
      }
    },
    computed: {
      ...mapGetters([
        'name',
        'mobile',
        'userId'
      ]),

      exportPurchaseReceipt(){
        if(this.roles.indexOf("exportPurchaseReceipt") != -1){
          return true
        }else{
          return false
        }
      },
    },
    created() {
      var now = new Date(); //当前日期
      var nowMonth = now.getMonth(); //当前月
      var nowYear = now.getFullYear(); //当前年
      //本月的开始时间
      var monthStartDate = new Date(nowYear, nowMonth, 1);
      //本月的结束时间
      var monthEndDate = new Date(nowYear, nowMonth+1, 0);
      var timeStar=Date.parse(monthStartDate);//s
      var timeEnd=Date.parse(monthEndDate);//s
      let endDate = formatDate(new Date(timeEnd), 'yyyy-MM-dd');
      let startDate = formatDate(new Date(timeStar), 'yyyy-MM-dd');
      this.listQuery.orderDate = [startDate,endDate];
      this.getList();
      this.getUserOptionList();
      this.getSupplierList();
      this.getOrderGoodsList();
    },
    filters: {
      fifState(val) {
        if(val == 0){
          return '未开票'
        }else if(val == 1){
          return '部分开票'
        }else if(val == 2){
          return '已开票'
        }
      },
    },
    methods:{
      //查看发货单
      toDetail(item){
        let routeData = this.$router.resolve({name:'inWarehouseDetail',query:{id:item.entryId}});
        window.open(routeData.href);
      },

      getOrderGoodsList(){
        orderGoodsList().then((res)=>{
          this.goodsList = res.list
        })
      },
      //供应商
      getSupplierList() {
        supplierList().then((res)=>{
          this.options2 = res;
        })
      },

      //获取员工下拉选项
      getUserOptionList(){
        getOrderUsersList().then((res)=>{
          this.userOptionList = res;
        })
      },


      handleSizeChange(val){
        this.listQuery.pageNum = 1;
        this.listQuery.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val){
        this.listQuery.pageNum = val;
        this.getList();
      },

      handleSearchList(){
        this.listQuery.pageNum = 1;
        this.getList();
      },

      //获取列表
      getList() {
        this.listLoading = true;
        let data = JSON.parse(JSON.stringify(this.listQuery));
        data.isExport = 0
        invoiceListPurchase(this.listQuery).then(response => {
          this.tableData = response.list;
          this.count = response.count;
          this.listLoading = false;
          this.orderAmount = response.summary.orderAmount;
          this.hasInvoiceAmount = response.summary.hasInvoiceAmount;
          this.unInvoiceAmount = response.summary.unInvoiceAmount;
          this.goodsNum = response.summary.goodsNum;
        }).catch(()=>{
          this.listLoading = false;
        });
      },

      //下载excle
      downloadExcel(){
        this.btnLoading2 = true;
        let params = JSON.parse(JSON.stringify(this.listQuery));
        params.isExport = 1;
        invoiceListPurchase(params).then((res)=>{
          this.btnLoading2 = false;
          require.ensure([], () => {
            const { export_json_to_excel } = require('sheet/Export2Excel');
            const tHeader = res.title;
            const filterVal = res.columns;
            const list = res.list;
            const data = this.formatJson(filterVal, list);
            export_json_to_excel(tHeader, data, res.fileName);
          })
        }).catch(()=>{
          this.btnLoading2 = false;
        })
      },

      //下载表格数据格式化
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },

    },
  }
</script>

<style scoped lang="scss">
  .upload-div{
    text-align: center;
    position:relative;
    #upload{

      opacity: 0;
      position: absolute;
      z-index: 1;
      width:154px;
      height:40px
    }
  }
</style>
